package com.cat.binaryTree;

import java.util.ArrayDeque;
import java.util.Deque;

/**
*   @description https://leetcode.cn/problems/find-bottom-left-tree-value/
*   @author 曲大人的喵
*   @create 2025/9/28 20:33
*   @since JDK17
*/

public class Solution83 {
    public int findBottomLeftValue(TreeNode root) {
        Deque<TreeNode> q = new ArrayDeque<>();
        q.add(root);
        int ans = 0;
        while (!q.isEmpty()) {
            for (int i = 0, end = q.size(); i < end; i++) {
                TreeNode node = q.poll();
                if (i == 0) {
                    ans = node.val;
                }
                if (node.left != null) {
                    q.addLast(node.left);
                }
                if (node.right != null) {
                    q.addLast(node.right);
                }
            }
        }

        return ans;
    }
}
